//
//  MineViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/21.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "Mine.h"
#import "LoginViewController.h"
#import "MyDownloadViewController.h"
#import "HistoryViewController.h"
#import "CollectViewController.h"
#import "FeedbackViewController.h"
#import "SettingViewController.h"


@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, retain) UIImageView * headerImageView;
@property (nonatomic, retain) UIButton *button1;
@end

@implementation MineViewController

- (void)dealloc
{
    [_button1 release];
    [_headerImageView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [[NSMutableArray alloc]init];
        NSString * path = [[NSBundle mainBundle]pathForResource:@"Mine" ofType:@"plist"];
        NSMutableArray * myArray = [NSMutableArray arrayWithContentsOfFile:path];
        for (NSDictionary * dic in myArray) {
            Mine * model = [[Mine alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            [model release];
        }
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self createView];
    UIButton * RegisterButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [RegisterButton setTitle:@"登录" forState:UIControlStateNormal];
    [RegisterButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [RegisterButton addTarget:self action:@selector(RegisterAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * RegisterButtonItem = [[UIBarButtonItem alloc]initWithCustomView:RegisterButton];
    self.navigationItem.rightBarButtonItem = RegisterButtonItem;
    [RegisterButton release];
    
}
- (void)RegisterAction:(id)sender
{
    LoginViewController * loginView = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginView animated:YES];
    [loginView release];
}

- (void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
//    self.tableView.rowHeight = 50;
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
       return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 160;
}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
//    [view setBackgroundColor:[UIColor greenColor]];
//    return view;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return self.array.count;
//    }
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellInde = @"cell";
    MineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellInde];
    if (!cell) {
        cell = [[[MineTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInde]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    Mine * model = [self.array objectAtIndex:indexPath.row];
    [cell.MyType setText:model.My];
    [cell.Image setImage:[UIImage imageNamed:model.MyImage]];
//    cell.layer.borderWidth = 0.25;
    
    return cell;
}

#pragma mark 首行cell设置

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160)];
    [headerView setBackgroundColor:[UIColor whiteColor]];
    
    
    
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 10, 110, 110)];
    [_headerImageView setImage:[UIImage imageNamed:@"0.png"]];
    
    
    [self.headerImageView.layer setCornerRadius:55];
    
    [self.headerImageView.layer setMasksToBounds:YES];
    
    _headerImageView.userInteractionEnabled = YES;
    [headerView addSubview:_headerImageView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_headerImageView addGestureRecognizer:tap];
    
    UILabel * UserLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, 120, 80,30)];
    [UserLabel setText:@""];
    
    [UserLabel setTextColor:[UIColor blackColor]];
    [headerView addSubview:UserLabel];
    [UserLabel release];
    [tap release];
    [_headerImageView release];
    
    self.button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, 120, 80, 40)];
    [_button1 setTitle:@"更换头像" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    [self.button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:_button1];
    
    [_button1 release];
    
    return headerView;
}

- (void)tapAction:(id)sender
{
    LoginViewController * login = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:login animated:YES];
}

- (void)viewDidLoad {
//    [self.button1 setHidden:YES];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)buttonAction:(id)sender
{
    dd = [[UIImagePickerController alloc] init];
    dd.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    dd.delegate = self;

    dd.allowsEditing = YES;
    [self presentViewController:dd animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [dd dismissViewControllerAnimated:YES completion:nil];
    UIImage *im = [info objectForKey:UIImagePickerControllerEditedImage];
    self.headerImageView.image = im;
//    [self.button1 setHidden:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 每行cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        MyDownloadViewController * My = [[MyDownloadViewController alloc]init];
        [self.navigationController pushViewController:My animated:YES];
    } else if (indexPath.row == 1)
    {
        HistoryViewController * History = [[HistoryViewController alloc]init];
        [self.navigationController pushViewController:History animated:YES];
    } else if (indexPath.row == 2)
    {
        CollectViewController * Collect = [[CollectViewController alloc]init];
        [self.navigationController pushViewController:Collect animated:YES];
    } else if (indexPath.row == 3)
    {
        LoginViewController * Login = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:Login animated:YES];
    } else if (indexPath.row == 4)
    {
//        FeedbackViewController * Feedback = [[FeedbackViewController alloc]init];
//        [self.navigationController pushViewController:Feedback animated:YES];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登陆提示" message:@"您还没有登录哦~请先登录..." delegate:self cancelButtonTitle:@"我再看看" otherButtonTitles:@"立即登陆", nil];
        [alert show];
        [alert release];
    } else if (indexPath.row == 5)
    {
        SettingViewController * Setting = [[SettingViewController alloc]init];
        [self.navigationController pushViewController: Setting animated:YES];
    } 
}

@end
